package cn.db101.view;

import cn.db101.dao.adminDao;
import cn.db101.dao.readerDao;
import cn.db101.model.admin;
import cn.db101.model.reader;
import cn.db101.util.DbUtil;
import cn.db101.util.StringUtil;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;

public class loginFrame {
	int item;
	JFrame jf = new JFrame();
	private JPanel contentPane;
	private JComboBox admin_style_comboBox;
	private JTextField account_textField;
	private JPasswordField password_textField;
	private DbUtil dbUtil=new DbUtil();
	

	

	/**
	 * Create the frame.
	 */
	public  void init() {

		jf.setTitle("登录主页面");
		
		jf.setIconImage(Toolkit.getDefaultToolkit().getImage(loginFrame.class.getResource("/icon/Libraries_64px_1176502_easyicon.net.png")));
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setBounds(100, 100, 1158, 805);
		
		JMenuBar menuBar = new JMenuBar();
		jf.setJMenuBar(menuBar);
		
		JMenu m1 = new JMenu("主界面");
		m1.setIcon(new ImageIcon(loginFrame.class.getResource("/icon/线性图书.png")));
		menuBar.add(m1);
		
		JMenu m2 = new JMenu("图书管理规定");
		m2.setIcon(new ImageIcon(loginFrame.class.getResource("/icon/线性老师.png")));
	
		
		menuBar.add(m2);
		
		JMenu m3 = new JMenu(" 图书查询");
		m3.setIcon(new ImageIcon(loginFrame.class.getResource("/icon/线性本子vg.png")));
		menuBar.add(m3);
		
		JMenu m4 = new JMenu(" \u6CE8\u518C");
		m4.setIcon(new ImageIcon(loginFrame.class.getResource("/icon/线性铅笔.png")));
		menuBar.add(m4);
		
		JMenu m5 = new JMenu("\u767B\u5F55");
		m5.setIcon(new ImageIcon(loginFrame.class.getResource("/icon/线性双指点击.png")));
		menuBar.add(m5);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		jf.setContentPane(contentPane);
		
		JLayeredPane layeredPane = new JLayeredPane();
		contentPane.add(layeredPane, BorderLayout.CENTER);
		
		JLayeredPane layeredPane_1 = new JLayeredPane();
		layeredPane_1.setBounds(49, 113, 344, 348);
		layeredPane.add(layeredPane_1);
		
		JLabel account_label = new JLabel("\u8D26    \u53F7\uFF1A");
		account_label.setFont(new Font("微软雅黑", Font.PLAIN, 17));
		account_label.setBounds(46, 43, 72, 18);
		layeredPane_1.add(account_label);
		
		account_textField = new JTextField();
		account_textField.setColumns(10);
		account_textField.setBounds(149, 42, 120, 24);
		layeredPane_1.add(account_textField);
		
		JLabel gender_label_1 = new JLabel("\u5BC6    \u7801\uFF1A");
		gender_label_1.setFont(new Font("微软雅黑", Font.PLAIN, 17));
		gender_label_1.setBounds(46, 106, 72, 18);
		layeredPane_1.add(gender_label_1);
		
		password_textField = new JPasswordField();
		password_textField.setBounds(149, 105, 120, 24);
		layeredPane_1.add(password_textField);
		
		JButton login_Button = new JButton("\u767B\u5F55");
		login_Button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if(item!=0) {
					login1(e);
				}
				else {
					login2(e);
				}
				
			}
		});
		login_Button.setBackground(Color.WHITE);
		login_Button.setForeground(Color.BLACK);
		login_Button.setIcon(new ImageIcon(loginFrame.class.getResource("/icon/login\u5BFC\u5165.png")));
		login_Button.setBounds(91, 233, 113, 27);
		layeredPane_1.add(login_Button);
		
		JLabel admin_style_label = new JLabel("\u8D26\u6237\u7C7B\u578B\uFF1A");
		admin_style_label.setFont(new Font("微软雅黑", Font.PLAIN, 17));
		admin_style_label.setBounds(46, 168, 85, 18);
		layeredPane_1.add(admin_style_label);
		
		final JComboBox admin_style_comboBox = new JComboBox();
		admin_style_comboBox.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				item=admin_style_comboBox.getSelectedIndex();
			}
		});
		
		admin_style_comboBox.setModel(new DefaultComboBoxModel(new String[] {"\u8BFB\u8005", "\u7BA1\u7406\u5458", "\u7CFB\u7EDF\u7BA1\u7406\u5458"}));
		admin_style_comboBox.setBounds(149, 167, 121, 24);
		layeredPane_1.add(admin_style_comboBox);
		
		JLabel label = new JLabel("");
		label.setBackground(Color.LIGHT_GRAY);
		label.setIcon(new ImageIcon(loginFrame.class.getResource("/img/sign.png")));
		label.setBounds(0, 0, 1250, 722);
		layeredPane.add(label);
	
		m1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				//销毁当前页面
				closeThis();
				//打开新窗体
				new mainFrame().init();
			}
		});	
		m2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				//销毁当前页面
				closeThis();
				//打开新窗体
				new regulationFrame().init();
			}
		});	
		m3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				//销毁当前页面
				closeThis();
				//打开新窗体
				new searchFrame().init();
			}
		});	
		m4.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				//销毁当前页面
				closeThis();
				//打开新窗体
				new registerFrame().init();
			}
		});	
		

		//设置JFrame居中显示
		jf.setLocationRelativeTo(null);
		//设置窗体可见
		jf.setVisible(true);	
		
	}
	
	/**
	 * 管理员登录事件处理
	 * @param e
	 */
	private void login1(ActionEvent evt) {
		String account=new String(this.account_textField.getText());
		String password=new String(this.password_textField.getPassword());
		if(StringUtil.isEmpty(account)) {
			JOptionPane.showMessageDialog(null,"账号不能为空！");
			return;
		}
		if(StringUtil.isEmpty(password)) {
			JOptionPane.showMessageDialog(null,"密码不能为空！");
			return;
		}
		admin adm=new admin(account,password);
		Connection con=null;
		try {
			con=dbUtil.getCon();
			admin currentUser= adminDao.login(con,adm);
			if (currentUser != null) {
				closeThis();
				new admin_mainFrame().init();
			} else {
				JOptionPane.showMessageDialog(null, "账号或密码错误！");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	
	}
	/**
	 * 读者登录事件处理
	 * @param evt
	 */
	private void login2(ActionEvent evt) {
		String reader_no=new String(this.account_textField.getText());
		String password=new String(this.password_textField.getPassword());
		if(StringUtil.isEmpty(reader_no)) {
			JOptionPane.showMessageDialog(null,"账号不能为空！");
			return;
		}
		if(StringUtil.isEmpty(password)) {
			JOptionPane.showMessageDialog(null,"密码不能为空！");
			return;
		}
		reader rea=new reader(reader_no,password);
		Connection con=null;
		try {
			con=dbUtil.getCon();
			reader currentUser= readerDao.login(con,rea);
			if(currentUser!=null) {
				closeThis();
				new reader_mainFrame().init();
			}else {
				JOptionPane.showMessageDialog(null, "账号或密码错误！");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	
	}
	public  void closeThis() {
		jf.dispose();
	}
	public static void main(String[] args) {
		new loginFrame().init();	
	}
}
